package com.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.entity.CheckerMdata;
import com.entity.PowerRateRecovery;
import com.entity.TeamInfo;
import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.code.AxisType;
import com.github.abel533.echarts.code.SeriesType;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.json.GsonOption;
import com.github.abel533.echarts.series.Bar;
import com.github.abel533.echarts.series.Line;
import com.github.abel533.echarts.style.ItemStyle;
import com.github.abel533.echarts.style.itemstyle.Normal;
import com.util.AppUtils;

/**
 *  @Description: 大屏展示业务接口实现类
 *  @author  SLH
 *  @date  20170228 
 *
 */
@Service("bigShowService")
@Transactional
public class BigshowServiceImpl  extends CommonServiceImpl implements IBigShowService{
	
	/**
	 * 班组电费回收情况
	 * @param bzId
	 * @return
	 */
	public String getTeamPowerRateRecovery(String bzId){
		//获取当前时间  
        Calendar cal = Calendar.getInstance();  
        //得到一个月最最后一天日期(31/30/29/28)  
        int MaxDay=cal.getActualMaximum(Calendar.DAY_OF_MONTH); 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");  
        String curMonth = sdf.format(cal.getTime());
        //班组编号
      //  String bzId = "";
        
//        if(StringUtils.isNotBlank(teamId) && AppUtils.isInteger(teamId)){
//        	TeamInfo teamInfo =  this.commonDao.get(TeamInfo.class, Integer.parseInt(teamId));
//        	bzId = teamInfo.getBzId();
//        }
        
        /**
         * 获取班组当月电费回收情况数据
         */
        List<PowerRateRecovery> prrLst = this.commonDao.findByQueryString("from PowerRateRecovery where bzId='"+bzId+"' and cDate like '"+curMonth+"%' order by c_date");

		GsonOption option = new GsonOption();
	    option.title("");
	    option.legend("回收率","欠费");
	    
	    option.toolbox().show(false);
	    option.calculable(true);
	    option.tooltip().trigger(Trigger.axis);
	    
	    //X轴设置
	    ValueAxis valueAxis = new ValueAxis();
	    List<String> data = new ArrayList();
	    for(int i=1;i<=MaxDay;i++){
	    	data.add(String.valueOf(i));
	    }
	    valueAxis.setType(AxisType.category);
	    valueAxis.setData(data);
	    option.xAxis(valueAxis);
	    
	    //Y轴设置
	    CategoryAxis hylAxis = new CategoryAxis();
	    hylAxis.setType(AxisType.value);
	    hylAxis.name("回收率(%)");
	   // hylAxis.min(0);
	   // hylAxis.max(100);
	    hylAxis.interval(10);
	    
	    CategoryAxis qfAxis = new CategoryAxis();
	    qfAxis.setType(AxisType.value);
	    qfAxis.name("欠费(万元)");
	   // qfAxis.min(0);
	   // qfAxis.max(1000);
	    qfAxis.interval(50);
	    
	    option.yAxis(hylAxis,qfAxis);
	     
	    //Series
	    Line hylLine = new Line();
	    hylLine.name("回收率");
	    hylLine.smooth(true).setType(SeriesType.line);
	    ItemStyle itemStyle = new ItemStyle();
	    Normal normal = new Normal();
	    normal.setColor("#90b8e4");
	    itemStyle.setNormal(normal);
	    hylLine.setItemStyle(itemStyle);
	   // hylLine.data("99.123","94.234","91.879","89.012","97.567");
	    
	    Line qfLine = new Line();
	    qfLine.name("欠费");  
	    qfLine.smooth(true).setType(SeriesType.line);
	    itemStyle = new ItemStyle();
	    normal = new Normal();
	    normal.setColor("#ff8f8f ");
	    itemStyle.setNormal(normal);
	    qfLine.setItemStyle(itemStyle);
	   // qfLine.data("123","360","87","566","388");  
	    qfLine.yAxisIndex(1);
	    
	    for(PowerRateRecovery prr:prrLst){
	    	 //添加回收率数据
	    	 hylLine.data(prr.getRateofrecovery());
	    	 //添加欠费数据
	    	 qfLine.data(prr.getArrearage());
	    }
	    
	    option.series(hylLine,qfLine);
		return option.toString();
	}
	
	/**
	 * 班组当月巡视情况
	 * @param bzId
	 * @return
	 */
	public String getInspections(String bzId){
		Calendar cal = Calendar.getInstance();  
		//上一个月
		cal.add(Calendar.MONTH, -1);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");  
        String prewMonth = sdf.format(cal.getTime());
        //班组编号
      //  String bzId = "";
        
//        if(StringUtils.isNotBlank(teamId) && AppUtils.isInteger(teamId)){
//        	TeamInfo teamInfo =  this.commonDao.get(TeamInfo.class, Integer.parseInt(teamId));
//        	bzId = teamInfo.getBzId();
//        }
        
        /**
         * 获取班组上月巡视情况数据
         */
        List<CheckerMdata> cmdLst = this.commonDao.findByQueryString("from CheckerMdata where bzId='"+bzId+"' and createMonth = '"+prewMonth+"' order by createMonth");
		
		GsonOption option = new GsonOption();
	    option.title("");
	    option.legend("计划巡视数","当月巡视数");
	    
	    option.toolbox().show(false);
	    option.calculable(true);
	    option.tooltip().trigger(Trigger.axis);
	    
	    //X轴设置
	    ValueAxis valueAxis = new ValueAxis();
	    List<String> data = new ArrayList();
	    
	    for(CheckerMdata cmd:cmdLst){
	    	data.add(cmd.getCheckerName());
	    }
	    valueAxis.setType(AxisType.category);
	    valueAxis.setData(data);
	    option.xAxis(valueAxis);
	    
	    //Y轴设置
	    CategoryAxis yAxis = new CategoryAxis();
	    yAxis.setType(AxisType.value);
	    
	    option.yAxis(yAxis);
	    
	    //Series
	    Bar jhBar = new Bar();
	    jhBar.name("计划巡视数");
	    jhBar.setType(SeriesType.bar);
	    ItemStyle itemStyle = new ItemStyle();
	    Normal normal = new Normal();
	    normal.setColor("#ff8f8f");
	    itemStyle.setNormal(normal);
	    jhBar.setItemStyle(itemStyle);
	   //jhBar.data("80","94","91","89","97");
	    
	    Bar sjBar = new Bar();
	    sjBar.name("当月巡视数");
	    sjBar.setType(SeriesType.bar);
	    itemStyle = new ItemStyle();
	    normal = new Normal();
	    normal.setColor("#90b8e4");
	    itemStyle.setNormal(normal);
	    sjBar.setItemStyle(itemStyle);
	    //sjBar.data("80","97","87","86","92");
	    
	    for(CheckerMdata cmd:cmdLst){
	    	sjBar.data(cmd.getInspections());
	    	jhBar.data(cmd.getPlanInspections());
	    }
	    
	    option.series(jhBar,sjBar);
	    return option.toString();
	}
	
	/**
	 * 班组重点工作情况
	 * @param bzId
	 * @return
	 */
	public List<Map<String,Object>> getKeyWork(String bzId){
		 
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT a.bz_id, ");
		sb.append("a.checker_name, ");
		sb.append("b.userCheckers, ");
		sb.append("a.inspections, ");
		sb.append("a.plan_inspections, ");
		sb.append("a.power_guarants, ");
		sb.append("a.emergency_repairs, ");
		sb.append("a.key_work, ");
		sb.append("a.settle_proportion, ");
	    sb.append("b.c_date, ");
	    sb.append("b.arrearage, ");
	    sb.append("b.rateofrecovery, ");
	    sb.append("b.summon, ");
	    sb.append("b.wos95598, ");
	    sb.append("b.wos12345, ");
	    sb.append("b.parlm_extension, ");
	    sb.append("b.low_extension, ");
	    sb.append("b.high_extension ");
       
	    sb.append("FROM ");
	    sb.append("(SELECT bz_id,checker_name,inspections,plan_inspections,power_guarants,emergency_repairs,key_work,settle_proportion ");
	    sb.append("FROM yl_checker_mdata WHERE bz_id = ? and c_month = (SELECT MAX(c_month) FROM yl_checker_mdata)) a LEFT JOIN ");
	    sb.append("(SELECT bz_id,checker_name,c_date,arrearage,rateofrecovery,summon,wos95598,wos12345,parlm_extension,low_extension,high_extension,userCheckers "); 
	    sb.append("FROM yl_checker_ddata WHERE bz_id = ? and c_date = (SELECT MAX(c_date) FROM yl_checker_ddata)) b ");
        sb.append("ON a.bz_id = b.bz_id  AND a.checker_name = b.checker_name  ");
       // sb.append("LEFT JOIN  yl_checker_info  c ");
       // sb.append("ON a.bz_id = c.bz_id  AND a.checker_name = c.checker_name AND c.bz_id = ? ");
        
//        System.out.println(sb.toString());
		
        
		List<Map<String,Object>> kwLst = this.commonDao.findForJdbc(sb.toString(), bzId,bzId);
		
		return  kwLst;
	}
	
	
	public static void main(String[] args){
		Calendar cal = Calendar.getInstance();  
		cal.add(Calendar.MONTH, -1); 
       //得到一个月最最后一天日期(31/30/29/28)  
        int MaxDay=cal.getActualMaximum(Calendar.DAY_OF_MONTH); 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");  
        String curMonth = sdf.format(cal.getTime());
		System.out.println(curMonth);
	}
	
}
